package com.gupaoedu.baifc.dynamicproxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * projectName: baifc_proxy
 * packageName: com.gupaoedu.baifc.dynamicproxy.cglib
 * Created: 2019/3/19.
 * Auther: baifc
 * Description: cglib模式
 */
public class CglibMeipo implements MethodInterceptor {


    public Object getInstance(Class clazz) {
        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);

        return enhancer.create();
    }


    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        before();
        Object obj = methodProxy.invokeSuper(o, objects);
        after();

        return obj;
    }

    private void before() {
        System.out.println("开始物色...");
    }

    private void after() {
        System.out.println("物色OK");
    }
}
